using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;

namespace Fusion.Frameworks.Editor
{
    public class Utility
    {
        [MenuItem("Utility/OpenPersistent")]
        public static void OpenPersistent()
        {
            System.Diagnostics.Process.Start("explorer.exe", Application.persistentDataPath.Replace("/", "\\"));
        }

        public static void CopyDirectory(string sourceDir, string destinationDir, bool recursive)
        {
            // Get information about the source directory
            var dir = new DirectoryInfo(sourceDir);

            // Check if the source directory exists
            if (!dir.Exists)
                throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}");

            // Cache directories before we start copying
            DirectoryInfo[] dirs = dir.GetDirectories();

            // Create the destination directory
            Directory.CreateDirectory(destinationDir);
            Debug.Log(destinationDir);
            // Get the files in the source directory and copy to the destination directory
            foreach (FileInfo file in dir.GetFiles())
            {
                string targetFilePath = Path.Combine(destinationDir, file.Name);
                Debug.Log(targetFilePath);
                file.CopyTo(targetFilePath);
            }

            // If recursive and copying subdirectories, recursively call this method
            if (recursive)
            {
                foreach (DirectoryInfo subDir in dirs)
                {
                    string newDestinationDir = Path.Combine(destinationDir, subDir.Name);
                    Debug.LogError(subDir.Name);
                    CopyDirectory(subDir.FullName, newDestinationDir, true);
                }
            }
        }

        public static void AppendScriptingDefineSymbol(string scriptingDefineSymbol)
        {
            if (!ContainScriptingDefineSymbol(scriptingDefineSymbol))
            {
                BuildTargetGroup buildTargetGroup = BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget);

                string scriptingDefineSymbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(buildTargetGroup);
                scriptingDefineSymbols = $"{scriptingDefineSymbols};{scriptingDefineSymbol}";
                PlayerSettings.SetScriptingDefineSymbolsForGroup(buildTargetGroup, scriptingDefineSymbols);
            }
        }

        public static bool ContainScriptingDefineSymbol(string scriptingDefineSymbol)
        {
            BuildTargetGroup buildTargetGroup = BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget);

            string scriptingDefineSymbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(buildTargetGroup);

            string[] array = GetScriptingDefineSymbolArray(scriptingDefineSymbols);
            return array.Contains(scriptingDefineSymbol);
        }

        public static void DeleteScriptingDefineSymbol(string scriptingDefineSymbol)
        {
            BuildTargetGroup buildTargetGroup = BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget);

            string scriptingDefineSymbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(buildTargetGroup);
            string[] array = GetScriptingDefineSymbolArray(scriptingDefineSymbols);

            string final = "";

            for (int index = 0; index < array.Length; index++)
            {
                string value = array[index];
                if (value != scriptingDefineSymbol)
                {
                    final += $"{value}";
                    if (index < array.Length - 1)
                    {
                        final += ";";
                    }
                }
            }
            PlayerSettings.SetScriptingDefineSymbolsForGroup(buildTargetGroup, final);
        }

        private static string[] GetScriptingDefineSymbolArray(string scriptingDefineSymbols)
        {
            return scriptingDefineSymbols.Split(";");
        }
    }
}


